const mongoose = require('mongoose');

// 定义 tagSchema
const tagSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        unique: true,
        trim: true,
        maxlength: 50
    },
    slug: {
        type: String,
        unique: true,
        trim: true
    },
    postCount: {
        type: Number,
        default: 0
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    updatedAt: {
        type: Date,
        default: Date.now
    }
});

// 自动生成 slug
tagSchema.pre('save', function (next) {
    if (this.name && !this.slug) {
        this.slug = this.name.toLowerCase().replace(/\s+/g, '-');
    }
    next();
});

// 创建并导出 Tag 模型
const Tag = mongoose.model('Tag', tagSchema);
module.exports = Tag;    